<template>
	<div class="system-menu-container layout-pd">
		<el-card shadow="hover">
			<div class="system-menu-search mb15">
				<el-row :gutter="20">
					<el-col :span="24" v-auths="['system:post:query']">
						<el-input size="default" placeholder="请输入岗位名称" class="mt10" v-model="state.param.postName">
						</el-input>
						<el-select v-model="state.param.status" placeholder="岗位状态" clearable class="mt10" size="default">
							<el-option v-for="dict in sys_normal_disable" :key="dict.value" :label="dict.label"
								:value="dict.value" />
						</el-select>
						<el-button size="default" type="primary" class="mt10" @click="getTableData">
							<el-icon>
								<ele-Search />
							</el-icon>
							查询
						</el-button>
						<el-button size="default" class="mt10" type="info" @click="resetParamData()">
							<el-icon>
								<ele-Refresh />
							</el-icon>
							重置
						</el-button>
					</el-col>
					<el-col :span="24">
						<el-button size="default" type="success" class="mt10" @click="onOpenAddPost('add')"
							v-auth="'system:post:add'">
							<el-icon>
								<ele-Plus />
							</el-icon>
							添加岗位
						</el-button>
					</el-col>
				</el-row>
			</div>
			<el-table :data="state.tableData.data" v-loading="state.tableData.loading" style="width: 100%">
				<el-table-column prop="postId" label="岗位编号" show-overflow-tooltip></el-table-column>
				<el-table-column prop="postCode" label="岗位编码" show-overflow-tooltip></el-table-column>
				<el-table-column prop="postName" label="岗位名称" show-overflow-tooltip></el-table-column>
				<el-table-column prop="postSort" label="显示顺序" show-overflow-tooltip></el-table-column>
				<el-table-column prop="createBy" label="创建人" show-overflow-tooltip></el-table-column>
				<el-table-column label="状态" show-overflow-tooltip width="80">
					<template #default="scope">
						<dict-tag :options="sys_normal_disable" :value="scope.row.status" />
					</template>
				</el-table-column>
				<el-table-column label="创建时间" show-overflow-tooltip>
					<template #default="scope">
						<span>{{ scope.row.createTime }}</span>
					</template>
				</el-table-column>
				<el-table-column label="操作" show-overflow-tooltip width="95" fixed="right"
					v-auths="['system:post:edit', 'system:post:remove']">
					<template #default="scope">
						<el-button size="small" text type="primary" @click="onOpenEditPost('edit', scope.row)"
							v-auth="'system:post:edit'">修改</el-button>
						<el-button size="small" text type="primary" @click="onTabelRowDel(scope.row)"
							v-auth="'system:post:remove'">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
		</el-card>
		<PostDialog ref="postDialogRef" @refresh="getTableData()" />
	</div>
</template>

<script setup lang="ts" name="systemPost">
import { defineAsyncComponent, ref, reactive, onMounted } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
import { usePost } from '/@/api/system/post';
import { useDict } from '/@/utils/dict';

const { sys_normal_disable } = useDict("sys_normal_disable");

// 引入组件
const PostDialog = defineAsyncComponent(() => import('/@/views/system/post/dialog.vue'));

// 定义变量内容
const postDialogRef = ref();
const state = reactive<TableType>({
	tableData: {
		// 表格树数据
		data: [],
		// 岗位树选项
		postOptions: [],
	},
	total: 0,
	loading: false,
	param: {
		pageNum: 1,
		pageSize: 10,
		postName: '',
		status: '',
	},
});

/**
 * 初始化表格数据
 * @returns
 */
const getTableData = async () => {
	state.tableData.loading = true;
	try {
		const res = await usePost().listPost(state.param);
		state.tableData.data = res.rows;
		state.total = res.total;
	} catch (error) {
		Promise.reject(error);
	}
	state.tableData.loading = false;
};

/**
 * 打开新增岗位弹窗
 * @param type
 * @param data 岗位树数据
 */
const onOpenAddPost = (type: string) => {
	postDialogRef.value.openDialog(type, {}, state.total);
};
/**
 * 打开编辑岗位弹窗
 * @param type
 * @param row 当前行数据
 */
const onOpenEditPost = (type: string, row: RowPostType) => {
	postDialogRef.value.openDialog(type, row);
};


/**
 * 删除岗位
 * @param row 当前行数据
 */
const onTabelRowDel = async (row: RowPostType) => {
	try {
		const confirmResult = await ElMessageBox.confirm(`此操作将永久删除岗位：${row.postName}, 是否继续?`, '提示', {
			confirmButtonText: '删除',
			cancelButtonText: '取消',
			type: 'warning',
		});
		if (confirmResult === 'confirm') {
			const response = await usePost().delPost(row.postId);
			ElMessage.success(response.msg);
			getTableData();
		} else {
			ElMessage.info('已取消删除');
		}
	} catch (error) {
		Promise.reject(error);
	}
};



/**
 * 重置查询参数
 */
const resetParamData = () => {
	state.param = {
		pageNum: 1,
		pageSize: 10,
		postName: '',
		status: '',
	};
	getTableData();
};



// 页面加载时
onMounted(() => {
	getTableData();
});
</script>
<style lang="scss" scoped>
.mt10 {
	max-width: 140px;
	margin: 10px 0 10px 10px;
}</style>